home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1993 / MacHack 1993.toast / MacHack™ 1987-1992 / MacHack™ '88 / Proceedings '88 / Feldt Advanced Mac Programming / Serial Port / lib src / commLib.c next >
Encoding:
C/C++ Source or Header  |  1987-10-26  |  10.4 KB  |  395 lines  |  [TEXT/KAHL]

  1. /*                         *  Commlib source *                                */
  2. /*                            ssg Commlib                            */
  3.  
  4. /*        Copyright © 1986,87 by small systems guild.  All rights reserved.    */
  5.  
  6. /*                Written to compile under Aztec C version 1.06h                 */
  7. /*                    and under Lightspeed C version 2.01                        */
  8.  
  9. #include    <extcomm.h>
  10.  
  11. Boolean    CheckPort(whichPort)        /* take care of data arriving at comm port  */
  12. TermData    *whichPort;
  13. {
  14.     Rect        visRect;
  15.     long        count = NULL;
  16.     char        inChar;
  17.     
  18.     SerGetBuf(whichPort->commPort->refin,&count);/* check current port status    */
  19.  
  20.     if (count < 1)                            /* if no characters waiting at port    */
  21.         return(FALSE);                        /* return immediately with FALSE    */
  22.  
  23.     SetPort(whichPort->commWindow);
  24.     CalcContentRect(whichPort->commWindow,&visRect);
  25.     ClipRect(&visRect);
  26.     if (count > 0) {                        /* if characters waiting at port    */
  27.         getu(whichPort->commPort,&inChar);    /* read next character from port    */
  28.         scrput(whichPort,inChar);            /* write it to the screen            */
  29.         count--;                            /* decrement count by one character */
  30.     }
  31.     return(TRUE);
  32. }
  33.  
  34. void CommKey(aWindow,keyChar,modifiers)
  35. WindowPtr    aWindow;
  36. char        keyChar;
  37. int            modifiers;
  38. {
  39.     unsigned char    outChar;
  40.     char            str[16];
  41.     
  42.     outChar = (unsigned char)keyChar;        /* char sent MAY equal char typed if*/
  43.                                             /* none of the cases below are true    */
  44.     if ((modifiers & cmdKey) && (outChar > 96) && (outChar < 123)) {        outChar -= 97;
  45.         outChar -= 96;
  46.     }
  47.  
  48.     switch (outChar) {                        /* handle special characters typed    */
  49.         case '`':                            /* if possible mac escape character    */
  50.             if (!(modifiers & cmdKey))        /* if command key was not held down */
  51.                 outChar = ESC;                /* set outChar equal to escape char    */
  52.             break;
  53.  
  54.         case BS:                            /* if it was a backspace (control H)*/
  55.             outChar = DEL;                    /* change it to a delete character    */
  56.             break;
  57.     }
  58.     putu(((TermData *)(GetWPort(aWindow)))->commPort,outChar);    /* send character out the comm port    */
  59. }
  60.  
  61. OSErr putu(thePort,outChar)                /* send a character out the comm port    */
  62. SPortPtr    thePort;
  63. char        outChar;
  64. {
  65.     long         numChars = 1;
  66.  
  67.     return(FSWrite(thePort->refout,&numChars,&outChar));
  68. }
  69.  
  70. OSErr getu(thePort,theChar)                /* get a character from the comm port    */
  71. SPortPtr    thePort;
  72. char        *theChar;
  73. {
  74.     char         inChar = 0;
  75.     long         numChars = 1;
  76.     OSErr        result;
  77.  
  78.     result = FSRead(thePort->refin,&numChars,&inChar);
  79.     *theChar = inChar;
  80.     return(result);
  81. }
  82.  
  83. OSErr setupport(whichPort)        /* init communications port to active status      */
  84. SerialPort    *whichPort;
  85. {
  86.     if (ValidPointer((Ptr)whichPort))
  87.         if (ValidPointer((Ptr)(whichPort->refin))) {
  88.                 SerReset(whichPort->refin,whichPort->baud | 
  89.                     whichPort->parity | whichPort->stopbits | 
  90.                     whichPort->databits);
  91.             return(noErr);
  92.         }
  93.     return(nilHandleErr);
  94. }
  95.  
  96. void ResetPort(whichPort)        /* reset communications port to inactive status    */
  97. SerialPort    *whichPort;
  98. {
  99.     if (ValidPointer((Ptr)whichPort))
  100.         if (ValidPointer((Ptr)(whichPort->refin)))
  101.             SerSetBuf(whichPort->refin,NULL,0);
  102. }
  103.  
  104. OSErr InitSPort(whichPort,which)        /* init ports and set to indicated one  */
  105. SerialPort    *whichPort;
  106. char        which;
  107. {
  108.     static char inbuf[2048];
  109.  
  110.     if (!ValidPointer((Ptr)whichPort))
  111.         return(nilHandleErr);
  112.  
  113.     switch(which) {
  114.         case 'A':
  115.         case 'a':
  116.             whichPort->out = "\P.aout";
  117.             whichPort->in = "\P.ain";
  118.             break;
  119.         case 'B':
  120.         case 'b':
  121.             whichPort->out = "\P.bout";
  122.             whichPort->in = "\P.bin";
  123.             break;
  124.         default:
  125.             return(nilHandleErr);
  126.     }
  127.     whichPort->baud = baud1200;
  128.     whichPort->parity = noParity;
  129.     whichPort->stopbits = stop20;
  130.     whichPort->databits =  data8;
  131.     OpenDriver(whichPort->out, &(whichPort->refout));
  132.     OpenDriver(whichPort->in, &(whichPort->refin));
  133.     setupport(whichPort);
  134.     SerSetBuf(whichPort->refin,inbuf,2048);
  135. }
  136.  
  137. void scrput(cWD,c)                    /* output a character to the window with    */
  138. TermData    *cWD;                    /* escape sequence expansion and emulation  */
  139. char         c;
  140. {
  141. register int    i;
  142.  
  143.     switch(cWD->escFlag) {
  144.         case 0:
  145.             switch(c) {
  146.                 case ESC:
  147.                     cWD->escFlag = 1;
  148.                     RETURN;
  149.                 case LINEFEED:
  150.                     InvertRect(&(cWD->cursRect));
  151.                     if (cWD->row < (cWD->visRows - 1))
  152.                         cWD->row++;
  153.                     else
  154.                         del_lin(cWD,0);
  155.                     break;
  156.                 case CR:
  157.                     EraseRect(&(cWD->cursRect));
  158.                     cWD->charBuf[cWD->row][cWD->col] = c;
  159.                     /*(cWD->row)++*/;
  160.                     cWD->col = 0;
  161.                     break;
  162.                 case BS:
  163.                     InvertRect(&(cWD->cursRect));
  164.                     if (cWD->col)
  165.                         cWD->col--;
  166.                     else {
  167.                         cWD->col = cWD->visCols - 1;
  168.                         if (cWD->row)
  169.                             cWD->row--;
  170.                         else
  171.                             cWD->row = cWD->visRows - 1;
  172.                     }
  173.                     break;
  174.                 case STD_RIGHTARROW :
  175.                     InvertRect(&(cWD->cursRect));
  176.                     if (cWD->col < cWD->visCols)
  177.                         cWD->col++;
  178.                     else {
  179.                         cWD->col = 0;
  180.                         if (cWD->row < cWD->visRows - 1)
  181.                             cWD->row++;
  182.                         else
  183.                             cWD->row = 0;
  184.                     }
  185.                     break;
  186.                 case HOMECURSOR:
  187.                     InvertRect(&(cWD->cursRect));
  188.                     cWD->col = cWD->row = 0;
  189.                     /*cWD->firstIdx = 0;*/
  190.                     for (i = 0; i < cWD->visRows; i++)
  191.                          cWD->charBuf[i][0] = (unsigned char)0;
  192.                     break;
  193.                 case TAB:
  194.                     c = ((cWD->col + 4) & ~3) - cWD->col;
  195.                     while(c--)
  196.                         scrput(cWD,' ');
  197.                     return;
  198.                 case CLEARSCREEN:
  199.                     EraseRect(&(cWD->commWindow->portRect));
  200.                     cWD->col = cWD->row = 0;
  201.                     /*cWD->firstIdx = 0;*/
  202.                     for (i = 0; i < cWD->visRows; i++)
  203.                          cWD->charBuf[i][0] = (unsigned char)0;
  204.                     break;
  205.                 case STD_UPARROW:                        
  206.                     InvertRect(&(cWD->cursRect));
  207.                     if (cWD->row > 0)
  208.                         cWD->row--;
  209.                     else
  210.                         cWD->row = cWD->visRows - 1;
  211.                     break;
  212.                 default:
  213.                     if (c < 0x20 || c >= 0x7f)
  214.                         return;
  215.                     EraseRect(&(cWD->cursRect));
  216.                     MoveTo((cWD->col*6)+1, (cWD->row*12)+9);
  217.                     cWD->charBuf[cWD->row][cWD->col] = c;
  218.                     DrawChar(c);
  219.                     if (cWD->col < cWD->visCols - 1)
  220.                         cWD->col++;
  221.                     else {
  222.                         cWD->col = 0;
  223.                         if (cWD->row < cWD->visRows)
  224.                             cWD->row++;
  225.                         else
  226.                             del_lin(cWD,0);
  227.                     }
  228.                     break;
  229.             }
  230.             if (cWD->row > cWD->visRows - 1) {
  231.                 cWD->row = cWD->visRows - 1;
  232.                 /*cWD->firstIdx++;*/
  233.             }
  234.             break;
  235.         case 1:
  236.             RETURN;
  237.             break;            /* throw away character after escape for now    */
  238.                             /* terminal emulation hook goes here and default*/
  239.     }
  240.     cWD->cursRect.left = cWD->col * 6;
  241.     cWD->cursRect.top = cWD->row * 12;
  242.     cWD->cursRect.right = cWD->cursRect.left + 7;
  243.     cWD->cursRect.bottom = cWD->cursRect.top + 13;
  244.     InvertRect(&(cWD->cursRect));
  245. }
  246.  
  247. void    del_lin(cWD,lin)                    /* delete line primitive    */
  248. TermData    *cWD;
  249. int            lin;
  250. {
  251.     Rect     rect;
  252.     int        i;
  253.  
  254.     rect.left = 0;
  255.     rect.right = cWD->commWindow->portRect.right - 15;
  256.     rect.top = lin * 12;
  257.     rect.bottom = (cWD->visRows) * 12;
  258.     ScrollRect(&rect,0,-12,cWD->updateRgn);
  259.     if (lin <= cWD->row)
  260.         (cWD->row)--;
  261.    /* cWD->firstIdx++;*/
  262.     for (i = 0; i < 80; i++) 
  263.         cWD->charBuf[cWD->row][i] = (unsigned char)0;
  264. }
  265.  
  266. void    ins_lin(cWD,lin)                    /* insert blank line primitive        */
  267. TermData    *cWD;
  268. int            lin;
  269. {
  270.     Rect rect;
  271.  
  272.     rect.left = 0;
  273.     rect.right = cWD->commWindow->portRect.right - 15;
  274.     rect.top = lin * 12;
  275.     rect.bottom = (cWD->visRows - 1) * 12;
  276.     ScrollRect(&rect,0,12,cWD->updateRgn);
  277. }
  278.  
  279. void    clr_eol(cWD)                        /* clear to end of line primitive    */
  280. TermData    *cWD;
  281. {
  282.     Rect rect;
  283.  
  284.     rect.left = cWD->col * 6;
  285.     rect.top = cWD->row * 12;
  286.     rect.right = cWD->commWindow->portRect.right - 15;
  287.     rect.bottom = rect.top + 13;
  288.     EraseRect(&rect);
  289. }
  290.  
  291. void    ins_char(cWD)                        /* insert character primitive        */
  292. TermData    *cWD;
  293. {
  294.     Rect rect;
  295.  
  296.     rect.left = cWD->col * 6;
  297.     rect.top = cWD->row * 12;
  298.     rect.right = cWD->commWindow->portRect.right - 15;
  299.     rect.bottom = rect.top + 13;
  300.     ScrollRect(&rect,6,0,cWD->updateRgn);
  301. }
  302.  
  303. void    del_char(cWD)                        /* delete character primitive        */
  304. TermData    *cWD;
  305. {
  306.     Rect rect;
  307.  
  308.     rect.left = cWD->col * 6;
  309.     rect.top = cWD->row * 12;
  310.     rect.right = cWD->commWindow->portRect.right - 15;
  311.     rect.bottom = rect.top + 13;
  312.     ScrollRect(&rect,-6,0,cWD->updateRgn);
  313. }
  314.  
  315. void StdCommMenus(baudMenu,portMenu)
  316. MenuHandle        *baudMenu,*portMenu;
  317. {
  318.     if (ValidPointer((Ptr)baudMenu)) {
  319.         *baudMenu = NewMenu(BAUDmenuID,"\PBaud");
  320.         AppendMenu(*baudMenu,"\P  300;  600;1200;1800;2400;3600;4800;7200;9600;19200;57600");
  321.         InsertMenu(*baudMenu,0);
  322.     }
  323.     if (ValidPointer((Ptr)portMenu)) {
  324.         *portMenu = NewMenu(PORTmenuID,"\PPort");
  325.         AppendMenu(*portMenu,"\PModem Port;Printer Port");
  326.         InsertMenu(*portMenu,0);
  327.     }
  328.     DrawMenuBar();
  329. }
  330.  
  331. OSErr SendStr(whichPort,linePtr)        /* send Pascal string out comm port  */
  332. SerialPort    *whichPort;
  333. char        *linePtr;
  334. {
  335.     long    count;
  336.     int        i;
  337.  
  338.     for (i = 1;i <= linePtr[0];i++) 
  339.         putu(whichPort,linePtr[i]);
  340.     putu(whichPort,CR);
  341.                                     /* alternative faster algorithm! */
  342. /*    count = linePtr[0];    
  343.     FSWrite(whichPort->refout,&count,linePtr);*/
  344. }
  345.  
  346. OSErr RecvStr(whichPort,linePtr)
  347. SerialPort    *whichPort;
  348. char        *linePtr;
  349. {
  350.     EventRecord    evt;
  351.     long        count,endTicks;
  352.     char        inChar;
  353.     int            mask,charTot = 0,patience = 0;
  354.     OSErr        result;
  355.  
  356.     mask = keyDownMask | autoKeyMask | mDownMask;
  357.     GetNextEvent(mask,&evt);
  358.     endTicks = evt.when + patience;
  359.     
  360.     do {
  361.         count = NULL;
  362.         SerGetBuf(whichPort->refin,&count);
  363.  
  364. /*        if (charTot + count > linePtr[0]) 
  365.             count = linePtr[0] - charTot;
  366.         result = FSRead(whichPort->refin,&count,&linePtr);
  367.         scrLine(whichPort,linePtr,(int)count);
  368.         charTot += count;*/            /* prototype faster algorithm! */
  369.  
  370.         while (count > 0) {
  371.             getu(whichPort,&inChar);
  372.             if (inChar == CR || inChar == LINEFEED || charTot == linePtr[0]) {
  373.                 linePtr[0] = charTot;
  374.                 return(0); /* TRUE */
  375.             }
  376.             charTot++;
  377.             count--;
  378.             linePtr[charTot] = inChar;
  379.             if (GetNextEvent(mask,&evt))
  380.                 return(-1); /* FALSE */
  381.             if (evt.when > endTicks)
  382.                 return(-1);    /* FALSE */
  383.         }
  384.     } while (!GetNextEvent(mask,&evt) && evt.when < endTicks);
  385.     return(-1);    /* FALSE */
  386. }
  387.  
  388. Boolean Logon(whichPort,linePtr,patience)
  389. TermData    *whichPort;
  390. char        *linePtr;
  391. int            patience;
  392. {
  393.     return(TRUE);
  394. }
  395.